#pragma once

#include "db/oracle/OCIDB.h"
#include "db/Transaction.h"


inline void oci_test()
{
    DBConfig config;
    config.Host = "localhost";
    config.Port = 1521;
    config.User = "huanghua";
    config.Password = "123456";
    config.Database = "orcl";
    config.Charset = "gbk";
    OCIDB db;
    auto bres = db.open(config);

    std::string create_sql = "CREATE TABLE HUANGHUA.MIXTEST" 
        "(INTFIELD NUMBER(*, 0) DEFAULT 0 NOT NULL ENABLE,"
            "FLOATFIELD FLOAT(126) DEFAULT 0 NOT NULL ENABLE,"
            "STRINGFIELD VARCHAR2(64) NOT NULL ENABLE,"
            "DATEFIELD DATE,"
            "TIMESTAMPFIELD TIMESTAMP(6)"
            ") SEGMENT CREATION IMMEDIATE"
        " PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING"
        " STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645"
        " PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)"
        " TABLESPACE USERS";
    
    {
        auto bb = db.exec("SET AUTOCOMMIT ON");
        if (!bb) {
            printf("%s\n", db.last_error().c_str());
        }
        db.exec("DROP TABLE HUANGHUA.MIXTEST");
        db.exec(create_sql);
    }


    {
        assert(1 == db.exec("insert into mixtest(IntField,FloatField,StringField,TimeStampField) "
            "values(?,?,?,to_date('2015-12-25,13:25:59','yyyy-mm-dd,hh24:mi:ss'))", 1, 3.14, "or 1=1"));
        char str[] = "string";
        assert(1 == db.exec("insert into mixtest(IntField,FloatField,StringField,DateField) "
            "values(?,?,?,to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh24:mi:ss'))", 2, 2.22, str));

    }

    {
        DBRows data;
        assert(db.query("select IntField,FloatField,StringField,DateField,TimeStampField from mixtest", data));
        assert(data.size() == 2);
        for (auto it = data.begin(); it != data.end(); ++it) {
            int IntField;
            float FloatField;
            std::string StringField;
            std::string DateField;
            char TimeStampField[32] = {};
            assert(it->scan(IntField, FloatField, StringField, DateField, TimeStampField));
            printf("%d %f %s %s %s\n", IntField, FloatField, StringField.c_str(), DateField.c_str(), TimeStampField);
        }
    }

    {
        assert(1 == db.exec("update mixtest set FloatField = ? where IntField = ?", 9.99, 1));
        DBRows data;
        assert(db.query("select FloatField from mixtest where IntField = 1", data));
        assert(data.size() == 1);
        assert(data[0]["FloatField"].to_double() == 9.99);
    }

    {
        assert(1 == db.exec("delete from mixtest where IntField = 1"));
    }

    std::cout << "oracle test end " << 1 << std::endl;

}